﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;
namespace DTcms.DAL
{
	/// <summary>
	/// 数据访问类:foreman_user
	/// </summary>
	public partial class foreman_user
	{
        private string databaseprefix; //数据库表名前缀
        
        public foreman_user(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }
	

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "foreman_user");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DTcms.Model.foreman_user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into " + databaseprefix + "foreman_user(");
			strSql.Append("fore_work,fore_settime,fore_areas,fore_info,fore_exp,fore_renzheng,fore_level,fore_grade,fore_safe,fore_isred,fore_istop,fore_sort,fore_creater,fore_updatetime,fore_from,user_id)");
			strSql.Append(" values (");
			strSql.Append("@fore_work,@fore_settime,@fore_areas,@fore_info,@fore_exp,@fore_renzheng,@fore_level,@fore_grade,@fore_safe,@fore_isred,@fore_istop,@fore_sort,@fore_creater,@fore_updatetime,@fore_from,@user_id)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@fore_work", SqlDbType.Int,4),
					new SqlParameter("@fore_settime", SqlDbType.DateTime),
					new SqlParameter("@fore_areas", SqlDbType.NVarChar,200),
					new SqlParameter("@fore_info", SqlDbType.NText),
					new SqlParameter("@fore_exp", SqlDbType.NVarChar,255),
					new SqlParameter("@fore_renzheng", SqlDbType.Int,4),
					new SqlParameter("@fore_level", SqlDbType.Int,4),
					new SqlParameter("@fore_grade", SqlDbType.NVarChar,50),
					new SqlParameter("@fore_safe", SqlDbType.VarChar,100),
					new SqlParameter("@fore_isred", SqlDbType.Int,4),
					new SqlParameter("@fore_istop", SqlDbType.Int,4),
					new SqlParameter("@fore_sort", SqlDbType.Int,4),
					new SqlParameter("@fore_creater", SqlDbType.Int,4),
					new SqlParameter("@fore_updatetime", SqlDbType.DateTime),
					new SqlParameter("@fore_from", SqlDbType.NVarChar,100),
					new SqlParameter("@user_id", SqlDbType.Int,4)};
			parameters[0].Value = model.fore_work;
			parameters[1].Value = model.fore_settime;
			parameters[2].Value = model.fore_areas;
			parameters[3].Value = model.fore_info;
			parameters[4].Value = model.fore_exp;
			parameters[5].Value = model.fore_renzheng;
			parameters[6].Value = model.fore_level;
			parameters[7].Value = model.fore_grade;
			parameters[8].Value = model.fore_safe;
			parameters[9].Value = model.fore_isred;
			parameters[10].Value = model.fore_istop;
			parameters[11].Value = model.fore_sort;
			parameters[12].Value = model.fore_creater;
			parameters[13].Value = model.fore_updatetime;
			parameters[14].Value = model.fore_from;
			parameters[15].Value = model.user_id;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DTcms.Model.foreman_user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update " + databaseprefix + "foreman_user set ");
			strSql.Append("fore_work=@fore_work,");
			strSql.Append("fore_settime=@fore_settime,");
			strSql.Append("fore_areas=@fore_areas,");
			strSql.Append("fore_info=@fore_info,");
			strSql.Append("fore_exp=@fore_exp,");
			strSql.Append("fore_renzheng=@fore_renzheng,");
			strSql.Append("fore_level=@fore_level,");
			strSql.Append("fore_grade=@fore_grade,");
			strSql.Append("fore_safe=@fore_safe,");
			strSql.Append("fore_isred=@fore_isred,");
			strSql.Append("fore_istop=@fore_istop,");
			strSql.Append("fore_sort=@fore_sort,");
			strSql.Append("fore_creater=@fore_creater,");
			strSql.Append("fore_updatetime=@fore_updatetime,");
			strSql.Append("fore_from=@fore_from,");
			strSql.Append("user_id=@user_id");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@fore_work", SqlDbType.Int,4),
					new SqlParameter("@fore_settime", SqlDbType.DateTime),
					new SqlParameter("@fore_areas", SqlDbType.NVarChar,200),
					new SqlParameter("@fore_info", SqlDbType.NText),
					new SqlParameter("@fore_exp", SqlDbType.NVarChar,255),
					new SqlParameter("@fore_renzheng", SqlDbType.Int,4),
					new SqlParameter("@fore_level", SqlDbType.Int,4),
					new SqlParameter("@fore_grade", SqlDbType.NVarChar,50),
					new SqlParameter("@fore_safe", SqlDbType.VarChar,100),
					new SqlParameter("@fore_isred", SqlDbType.Int,4),
					new SqlParameter("@fore_istop", SqlDbType.Int,4),
					new SqlParameter("@fore_sort", SqlDbType.Int,4),
					new SqlParameter("@fore_creater", SqlDbType.Int,4),
					new SqlParameter("@fore_updatetime", SqlDbType.DateTime),
					new SqlParameter("@fore_from", SqlDbType.NVarChar,100),
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.fore_work;
			parameters[1].Value = model.fore_settime;
			parameters[2].Value = model.fore_areas;
			parameters[3].Value = model.fore_info;
			parameters[4].Value = model.fore_exp;
			parameters[5].Value = model.fore_renzheng;
			parameters[6].Value = model.fore_level;
			parameters[7].Value = model.fore_grade;
			parameters[8].Value = model.fore_safe;
			parameters[9].Value = model.fore_isred;
			parameters[10].Value = model.fore_istop;
			parameters[11].Value = model.fore_sort;
			parameters[12].Value = model.fore_creater;
			parameters[13].Value = model.fore_updatetime;
			parameters[14].Value = model.fore_from;
			parameters[15].Value = model.user_id;
			parameters[16].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from " + databaseprefix + "foreman_user ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from " + databaseprefix + "foreman_user ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DTcms.Model.foreman_user GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,fore_work,fore_settime,fore_areas,fore_info,fore_exp,fore_renzheng,fore_level,fore_grade,fore_safe,fore_isred,fore_istop,fore_sort,fore_creater,fore_updatetime,fore_from,user_id from " + databaseprefix + "foreman_user ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			DTcms.Model.foreman_user model=new DTcms.Model.foreman_user();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DTcms.Model.foreman_user GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,fore_work,fore_settime,fore_areas,fore_info,fore_exp,fore_renzheng,fore_level,fore_grade,fore_safe,fore_isred,fore_istop,fore_sort,fore_creater,fore_updatetime,fore_from,user_id from " + databaseprefix + "foreman_user ");
            if (!string.IsNullOrEmpty(where))
            {
                strSql.Append(" where " + where);
            }

            DTcms.Model.foreman_user model = new DTcms.Model.foreman_user();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DTcms.Model.foreman_user DataRowToModel(DataRow row)
		{
			DTcms.Model.foreman_user model=new DTcms.Model.foreman_user();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["fore_work"]!=null && row["fore_work"].ToString()!="")
				{
					model.fore_work=int.Parse(row["fore_work"].ToString());
				}
				if(row["fore_settime"]!=null && row["fore_settime"].ToString()!="")
				{
					model.fore_settime=DateTime.Parse(row["fore_settime"].ToString());
				}
				if(row["fore_areas"]!=null)
				{
					model.fore_areas=row["fore_areas"].ToString();
				}
				if(row["fore_info"]!=null)
				{
					model.fore_info=row["fore_info"].ToString();
				}
				if(row["fore_exp"]!=null)
				{
					model.fore_exp=row["fore_exp"].ToString();
				}
				if(row["fore_renzheng"]!=null && row["fore_renzheng"].ToString()!="")
				{
					model.fore_renzheng=int.Parse(row["fore_renzheng"].ToString());
				}
				if(row["fore_level"]!=null && row["fore_level"].ToString()!="")
				{
					model.fore_level=int.Parse(row["fore_level"].ToString());
				}
				if(row["fore_grade"]!=null)
				{
					model.fore_grade=row["fore_grade"].ToString();
				}
				if(row["fore_safe"]!=null)
				{
					model.fore_safe=row["fore_safe"].ToString();
				}
				if(row["fore_isred"]!=null && row["fore_isred"].ToString()!="")
				{
					model.fore_isred=int.Parse(row["fore_isred"].ToString());
				}
				if(row["fore_istop"]!=null && row["fore_istop"].ToString()!="")
				{
					model.fore_istop=int.Parse(row["fore_istop"].ToString());
				}
				if(row["fore_sort"]!=null && row["fore_sort"].ToString()!="")
				{
					model.fore_sort=int.Parse(row["fore_sort"].ToString());
				}
				if(row["fore_creater"]!=null && row["fore_creater"].ToString()!="")
				{
					model.fore_creater=int.Parse(row["fore_creater"].ToString());
				}
				if(row["fore_updatetime"]!=null && row["fore_updatetime"].ToString()!="")
				{
					model.fore_updatetime=DateTime.Parse(row["fore_updatetime"].ToString());
				}
				if(row["fore_from"]!=null)
				{
					model.fore_from=row["fore_from"].ToString();
				}
				if(row["user_id"]!=null && row["user_id"].ToString()!="")
				{
					model.user_id=int.Parse(row["user_id"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select * FROM view_foreman_user");
            if (strWhere.Trim() != "")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" * ");
            strSql.Append(" FROM view_foreman_user");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}


        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select (select count(1) from dt_article where category_id=52 and user_id=a.user_id)as baojia,(select count(1) from dt_article where category_id=53 and user_id=a.user_id)as anli,(select count(1) from dt_article where category_id=54 and user_id=a.user_id)as qianyue,(select count(1) from dt_foreman_apply where foreman_id=a.id)as yuyue, a.* FROM view_foreman_user as a");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + databaseprefix + "foreman_user set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
	}
}

